package com.ruoyi.system.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.ListOrdersFordTemp;
import com.ruoyi.system.mapper.ListOrdersFordTempMapper;
import com.ruoyi.system.service.IListOrdersFordTempService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 客户订单Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-11-21
 */
@Service
public class ListOrdersFordTempServiceImpl implements IListOrdersFordTempService
{
    private static final Logger log = LoggerFactory.getLogger(ListOrdersFordTempServiceImpl.class);

    @Autowired
    private ListOrdersFordTempMapper listOrdersFordTempMapper;

    /**
     * 查询客户订单
     * 
     * @param id 客户订单主键
     * @return 客户订单
     */
    @Override
    public ListOrdersFordTemp selectListOrdersFordTempById(Long id)
    {
        return listOrdersFordTempMapper.selectListOrdersFordTempById(id);
    }

    /**
     * 查询客户订单
     *
     * @param code 客户订单编码
     * @return 客户订单
     */
    public ListOrdersFordTemp selectListOrdersFordTempByCode(String code)
    {
        return listOrdersFordTempMapper.selectListOrdersFordTempByCode(code);
    }

    /**
     * 查询客户订单列表
     * 
     * @param listOrdersFordTemp 客户订单
     * @return 客户订单
     */
    @Override
    public List<ListOrdersFordTemp> selectListOrdersFordTempList(ListOrdersFordTemp listOrdersFordTemp)
    {
        return listOrdersFordTempMapper.selectListOrdersFordTempList(listOrdersFordTemp);
    }

    /**
     * 新增客户订单
     * 
     * @param listOrdersFordTemp 客户订单
     * @return 结果
     */
    @Override
    @Transactional
    public int insertListOrdersFordTemp(ListOrdersFordTemp listOrdersFordTemp)
    {
        listOrdersFordTemp.setCreateTime(DateUtils.getNowDate());
        listOrdersFordTemp.setCreateBy(SecurityUtils.getUsername());
        listOrdersFordTemp.setStatus(UserConstants.NORMAL);
        return listOrdersFordTempMapper.insertListOrdersFordTemp(listOrdersFordTemp);
    }

    /**
     * 修改客户订单
     * 
     * @param listOrdersFordTemp 客户订单
     * @return 结果
     */
    @Override
    @Transactional
    public int updateListOrdersFordTemp(ListOrdersFordTemp listOrdersFordTemp)
    {
        listOrdersFordTemp.setUpdateTime(DateUtils.getNowDate());
        listOrdersFordTemp.setUpdateBy(SecurityUtils.getUsername());
        return listOrdersFordTempMapper.updateListOrdersFordTemp(listOrdersFordTemp);
    }

    /**
     * 批量删除客户订单
     *
     * @param ids 需要删除的客户订单主键
     * @return 结果
     */
    @Override
    public int deleteListOrdersFordTempByIds(Long[] ids) {
        return listOrdersFordTempMapper.deleteListOrdersFordTempByIds(ids);
    }

    /**
     * 删除客户订单信息
     * 
     * @param id 客户订单主键
     * @return 结果
     */
    @Override
    public int deleteListOrdersFordTempById(Long id)
    {
        return listOrdersFordTempMapper.deleteListOrdersFordTempById(id);
    }

    /**
     * 导入客户订单数据
     *
     * @param ordersFordTempList 客户订单数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importOrdersFordTemp(List<ListOrdersFordTemp> ordersFordTempList, boolean isUpdateSupport, String operName) {

        if (StringUtils.isNull(ordersFordTempList) || ordersFordTempList.size() == 0)
        {
            throw new CustomException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        //String password = configService.selectConfigByKey("sys.user.initPassword");
        for (ListOrdersFordTemp listOrdersFordTemp : ordersFordTempList)
        {
            try
            {
                // 验证是否存在这个客户订单
                ListOrdersFordTemp loft = listOrdersFordTempMapper.selectListOrdersFordTempByCode(listOrdersFordTemp.getModelCode());
                if (StringUtils.isNull(loft))
                {
                    listOrdersFordTemp.setCreateBy(operName);
                    this.insertListOrdersFordTemp(listOrdersFordTemp);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、客户订单-车型 " + listOrdersFordTemp.getModel() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    listOrdersFordTemp.setUpdateBy(operName);
                    this.updateListOrdersFordTemp(listOrdersFordTemp);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、客户订单-车型 " + listOrdersFordTemp.getModel() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、客户订单-车型 " + listOrdersFordTemp.getModel() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、客户订单-车型 " + listOrdersFordTemp.getModel() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }



}
